package com.auto.api.core.type.classtotorna;

import cn.torna.sdk.param.DocParamResp;
import com.auto.api.core.entity.ClassToTornaContext;
import com.auto.api.core.entity.ProcessProp;
import com.auto.api.core.entity.PropertyContext;
import com.auto.api.core.type.TypeFactory;
import com.auto.api.core.util.BeanUtil;
import java.beans.PropertyDescriptor;
import java.util.List;

/**
 * @program:
 * @description: 类型转换接口
 * @author: liangshf
 * @create: 2020-09-24
 */
public class ClassToTornaResultApiHandler extends AbstractClassToTornaHandler {

    @Override
    public ProcessProp<ClassToTornaContext> getProcessProp(ClassToTornaContext context,
        PropertyContext propertyContext) {
        return new ProcessProp(propertyContext.getJudgePropType(), context, propertyContext);
    }

    @Override
    public void processProp(ProcessProp<ClassToTornaContext> processProp) {
        PropertyDescriptor prop = processProp.getPropertyContext().getProp();
        //多个继承泛型替换
        if (TypeFactory.isSimpleTypeHandler(processProp.getPropType())) {
            if (processProp.getPropertyContext().isCollectionType()) {
                BeanUtil.getTornaPostListField(processProp.getContext().getClazz(), processProp.getContext().getResult(), prop.getName());
                return;
            }
            BeanUtil.getFiledTornaApi(processProp.getContext().getClazz(), processProp.getContext().getResult(), prop);
        } else {
            List<DocParamResp> responseParams = getResult(new ClassToTornaContext(processProp.getPropertyContext().isObjParse() ? processProp.getPropertyContext().getGenPropResType() : processProp.getPropertyContext().getPropResType()));
            DocParamResp queryCreateParamReq = BeanUtil.getDocResponseResp(processProp.getPropertyContext().isCollectionType() ? "array" : "object", prop.getName());
            BeanUtil.setObjDescription(processProp.getContext().getClazz(), prop, queryCreateParamReq);
            queryCreateParamReq.setChildren(responseParams);
            processProp.getContext().getResult().add(queryCreateParamReq);
        }
    }
}
